package com.zmytest.model;

import com.zmytest.dto.RoleDto;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import java.util.List;

/**
 * Created on 2018/12/19.
 *
 * @author zhangmingyu
 */
@EqualsAndHashCode(callSuper = true)
@Data
@Entity
@NoArgsConstructor
@AllArgsConstructor
public class Role extends BaseModel<Long>{

    private String name;

    @ManyToMany
    @JoinTable(
            name = "role_permission",
            joinColumns = {@JoinColumn(name = "role_id")},
            inverseJoinColumns = {@JoinColumn(name = "permission_id")}
    )
    private List<Permission> permissions;

    public void update(RoleDto roleDto,List<Permission> permissions){
        this.name = ObjectUtils.isEmpty(roleDto.getName())? this.name:roleDto.getName();
        this.permissions = CollectionUtils.isEmpty(permissions)? this.permissions:permissions;
    }
}
